<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学生姓名" prop="studentName">
        <el-input
          v-model="queryParams.studentName"
          placeholder="请输入学生姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年级" prop="gradeName">
        <el-input
          v-model="queryParams.gradeName"
          placeholder="请输入年级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="班级" prop="className">
        <el-input
          v-model="queryParams.className"
          placeholder="请输入班级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <!-- <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:records:remove']"
        >批量删除</el-button> -->
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border stripe :header-cell-style="rowClass" v-loading="loading" :data="recordsList" @selection-change="handleSelectionChange">
      <!-- <el-table-column :resizable="false" type="selection" width="55" align="center" /> -->
      <el-table-column :resizable="false" label="学生姓名" align="center" prop="studentName" />
      <el-table-column :resizable="false" label="年级" align="center" prop="gradeName" />
      <el-table-column :resizable="false" label="班级" align="center" prop="className" />
      <el-table-column :resizable="false" label="早餐次数" align="center" prop="breakfast" />
      <el-table-column :resizable="false" label="午餐次数" align="center" prop="lunch" />
      <el-table-column :resizable="false" label="晚餐次数" align="center" prop="dinner" />
      <el-table-column :resizable="false" label="退款总金额" align="center" prop="refundAmount" />
      <el-table-column :resizable="false" label="学校审批状态" align="center" prop="schoolApprovalStatus" >
        <template slot-scope="scope">
          <div v-if="scope.row.schoolApprovalStatus==0" class="flex gap-2">
            <el-tag type="info" effect="dark" >
              待审核
            </el-tag>
          </div>
          <div v-else-if="scope.row.schoolApprovalStatus==1" class="flex gap-2">
            <el-tag type="success" effect="dark" >
              审批通过
            </el-tag>
          </div>
          <div v-else class="flex gap-2">
            <el-tag type="danger" effect="dark" >
              拒绝
            </el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column :resizable="false" label="学校审批时间" align="center" prop="schoolApprovalTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.schoolApprovalTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :resizable="false" label="学校审批意见" align="center" prop="schoolComment" />
      <el-table-column :resizable="false" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-refresh-left"
            @click="handleRefund(scope.row)"
          >退款审批</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:leaves:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改请假审批记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="学生姓名" prop="studentName">
          <el-input disabled v-model="form.studentName" />
        </el-form-item>
        <el-form-item disabled label="早餐次数" prop="breakfast">
          <el-input disabled v-model="form.breakfast" />
        </el-form-item>
        <el-form-item label="午餐次数" prop="lunch">
          <el-input disabled v-model="form.lunch" />
        </el-form-item>
        <el-form-item label="晚餐次数" prop="dinner">
          <el-input disabled v-model="form.dinner"/>
        </el-form-item>
        <el-form-item label="退款总金额" prop="refundAmount">
          <el-input disabled v-model="form.refundAmount" />
        </el-form-item>
        <el-form-item label="请假时间" prop="levelTime">
          <el-input disabled v-model="form.levelTime" />
        </el-form-item>
        <el-form-item label="是否退款" prop="payoutStatus">
          <el-radio v-model="payoutStatus" label='1'>同意</el-radio>
          <el-radio v-model="payoutStatus" label='0'>不同意</el-radio>
        </el-form-item>
        <el-form-item v-show="payoutStatus=='0'" label="备注" prop="payoutRemarks">
          <el-input  v-model="form.payoutRemarks" placeholder="请输入原因" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRecords, getRecords, delRecords, addRecords, updateRecords } from "@/api/leaveSystem/leaveRecord";
import { shenhetuikuan } from "@/api/financialManagement/refundApproval.js"

export default {
  name: "Records",
  data() {
    return {
      payoutStatus:'1',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中学生名字数组
      studentNames: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 请假审批记录表格数据
      recordsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        leaveId: null,
        schoolId: null,
        schoolApproverId: null,
        isDeleted: null,
        parentApprovalStatus: null,
        schoolApprovalStatus: null,
        schoolApprovalTime: null,
        schoolComment: null,
        studentName:null,
        className:null,
        gradeName:null
      },
      // 表单参数
      form: {
        payoutRemarks:""
      },
      // 表单校验
      rules: {
        payoutRemarks: [
          { required: true, message: "原因不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //设置table标题样式
    rowClass ({ row, rowIndex }) {
      return 'font-size: 15px; background-color: #FAFAFA;'
    },
    /** 查询请假审批记录列表 */
    getList() {
      this.loading = true;
      this.queryParams.payoutStatus = 2;
      this.queryParams.status = 1;
      listRecords(this.queryParams).then(response => {
        this.recordsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        leaveId: null,
        schoolId: null,
        schoolApproverId: null,
        isDeleted: null,
        parentApprovalStatus: null,
        schoolApprovalStatus: null,
        schoolApprovalTime: null,
        schoolComment: null,
        studentName:null,
        className:null,
        gradeName:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.studentNames=selection.map(item => item.studentName)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    //退款按钮
    handleRefund(row){
      this.reset();
      this.form=row
      this.form.levelTime=row.startTime+" 至 "+row.endTime
      console.log("===",row);
      this.open=true
      this.title="退款审批"
    },
    /** 提交按钮 */
    submitForm() {
      this.form.payoutStatus=this.payoutStatus
      console.log(this.form);
      if(this.payoutStatus=='0'){
      this.$refs["form"].validate(valid => {
        if (valid) {
          shenhetuikuan(this.form).then(response => {
            this.$modal.msgSuccess("审核成功");
            this.open = false;
            this.getList();
          });
        }
      });
    }else{
      shenhetuikuan(this.form).then(response => {
            this.$modal.msgSuccess("审核成功");
            this.open = false;
            this.getList();
          });
    }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const name=row.studentName || this.studentNames
      // this.$modal.confirm('是否确认删除请假审批记录编号为"' + ids + '"的数据项？').then(function() {
      this.$modal.confirm('是否确认删除请假审批姓名为"' + name + '"的数据项？').then(function() {
        return delRecords(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/records/export', {
        ...this.queryParams
      }, `records_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
